define(['jquery',"bbcare","comm/datatable/datatable","bld/app/classify/classifyList.servers","bld/app/classify/classifyList.ui"],function($,$bb,$datatable,$servers,$ui) {
    var classifyList = {};

    /**
     * 删除方案分类成功后的回调函数
     */
    var ajaxDeleteSchemeClass = function(data) {
        $bb.toastShow('删除成功');
        $("#showDelIcon").html("删除");
        classifyList.init();
    }

    /**
     * 确认删除方案分类
     */
    var confirmDeleteSchemeClass = function(params) {
        $servers.ajaxDeleteSchemeClass(params,ajaxDeleteSchemeClass);
    }

    /**
     * 删除某个方案分类
     */
    var onDelClassify = function() {
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var channelId = $bb.getChannelId();
        var classifyName = $(this).next().children().html();
        var schemeClassIds = $(this).next().attr('id');
        var params = {
            "oprInfo":{
                "opCode": "2065",
                "opType": "3",
                "opNote": "【" + doctorName + "】医生删除方案分类【" + classifyName + "】",
                "channelType": "2",
                "channelId": channelId
            },
            "schemeClassIds":schemeClassIds
        };
        $bb.toastConfirmShow(confirmDeleteSchemeClass,params,"确认是否删除？");
    }

    /**
     * 删除
     */
    var onDelClick = function() {
        var str = $("#showDelIcon").html() == "删除" ? "取消" : "删除";
        $("#showDelIcon").html(str);
        var dom = $("#classifyList").children();
        dom.each(function(i, e) {
            $(e).find('i').toggle();
            $(e).find('i').unbind("click");
            $(e).find('i').click(onDelClassify);
        });
    }

    /**
     * 添加方案分类成功后的回调函数
     */
    var ajaxAddSchemeClassCallBack = function(data) {
        $('.classifyListModal').modal('hide');
        $bb.toastShow('添加方案分类成功');
        classifyList.init();
    }

    /**
     * 添加方案分类
     */
    var onAddSchemeClass = function(){
        var classifyName = $("#classifyName").val();
        if( classifyName == '' ) {
            $bb.toastShow('请填写方案库名称');
        }else{
            var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
            var channelId = $bb.getChannelId();
            var diseaseId = $("#disease").val();
            var libId = $("#disease option:selected").attr("data-libId");
            var params = {
                "oprInfo":{
                    "opCode": "2063",
                    "opType": "3",
                    "opNote": "【" + doctorName + "】医生添加方案分类【" + classifyName + "】",
                    "channelType": "2",
                    "channelId": channelId
                },
                "schemeClass":{
                    "classType": '01',
                    "libId": libId,
                    "diseaseId": diseaseId,
                    "className": classifyName
                }
            };
            $servers.ajaxAddSchemeClass(params,ajaxAddSchemeClassCallBack);
        }
    }

    /**
     * 查询疾病成功后的回调函数
     */
    var ajaxDiseaseAllCallBack = function(data){
        var className = "classifyListModal";
        $bb.modal({
            "className": className,
            "tit": "添加方案分类",
            "size":"modal-sm"
        }, function() {
            $ui.add({
                "dom": $("." + className + " .modal-body"),
                "list":data.rows
            });
            $("." + className).modal("show");
            $("#add").unbind('click');
            $("#add").click(onAddSchemeClass);
        });
    }

    /**
     * 新增方案分类
     */
    var onAddClick = function() {
        $servers.ajaxDiseaseAll(ajaxDiseaseAllCallBack);
    }

    /**
     * 跳转到方案列表页面
     */
    var onJump = function() {
        var schemeClasses = $(this).data("schemeClasses");
        var classId = $(this).attr('id');
        var obj = $servers.getClassByClassId(schemeClasses,classId);
        $bb.openPage('bld_programList',obj);
    }

    /**
     * 查询方案分类成功后的回调函数
     */
    var ajaxQueryAllSchemeClassCallBack = function(data) {
        $ui.schemeClass({
            "id": "classifyList",
            "list": data.schemeClassInfos
        });
        $("#classifyList .classify-box").unbind('click');
        $("#classifyList .classify-box").data("schemeClasses",data.schemeClassInfos);
        $("#classifyList .classify-box").click(onJump);
    }

    classifyList.init = function(){
        $servers.ajaxQueryAllSchemeClass(ajaxQueryAllSchemeClassCallBack);
        // 新增
        $("#addBtn").unbind("click");
        $("#addBtn").click(onAddClick);
        // 删除
        $("#showDelIcon").unbind("click");
        $("#showDelIcon").click(onDelClick);
    };

    return classifyList;
});